
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Html
{
    using AngleSharp.Dom;
    using System.Diagnostics;

    /// <summary>
    /// 抓取属性
    /// </summary>
    public class HtmlFetchingProperty
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 选择器
        /// </summary>
        public string sel { get; set; }
        /// <summary>
        /// 属性
        /// </summary>
        public string attr { get; set; }
        /// <summary>
        /// 将属性值字符串切割,[起始索引,结束索引],结束索引以最后一个字符为基准。
        /// </summary>
        public string sub { get; set; }
        /// <summary>
        /// 内部属性，设定了内部属性后当前属性的结果为list。
        /// </summary>
        public HtmlFetchingProperty[] properties { get; set; }

        /// <summary>
        /// 从指定的标签中获取当前属性的值
        /// </summary>
        /// <param name="pnode"></param>
        public HtmlFetchingPropertyValue GetValue(IParentNode pnode)
        {
            HtmlFetchingPropertyValue pv = new HtmlFetchingPropertyValue() { name = this.name };
            if (properties != null && properties.Length > 0)
            {
                var eles = pnode.QuerySelectorAll(sel);
                List<object> l = new List<object>();
                foreach (var ele in eles)
                {
                    HtmlFetchingPropertyValue[] array = new HtmlFetchingPropertyValue[properties.Length];
                    int i = 0;
                    foreach (var prop in properties)
                    {
                        array[i] = prop.GetValue(ele);
                        i++;
                    }
                    l.Add(array);
                }
                pv.value = l.ToArray();
            }
            else
            {
                IElement ele;
                if (string.IsNullOrWhiteSpace(this.sel))
                {//未指定选择器则直接使用父节点
                    ele = pnode as IElement;
                }
                else
                {
                    ele = pnode.QuerySelector(this.sel);
                }

                if (ele == null)
                {
                    pv.value = null;
                    //Debug.Fail("ddd");
                }
                else
                {
                    pv.value = this.getAttr(ele, attr);
                }

            }
            return pv;
        }

        private string getAttr(IElement element, string att)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element", "不能为null。");
            }
            string value = "";
            if (string.Compare(att, "html", true) == 0)
            {
                value = element.InnerHtml;
            }
            else if (string.Compare(att, "text", true) == 0)
            {
                value = element.TextContent;
            }
            else if (string.Compare(att, "content", true) == 0)
            {
                value = element.TextContent;
            }
            else
            {
                value = element.GetAttribute(att);
            }
            if (string.IsNullOrWhiteSpace(sub))
            {
                return value;
            }
            var subs = sub.Split(',');
            int start = 0, end = 0;
            if (subs.Length >= 1)
            {
                start = int.Parse(subs[0]);
            }
            if (subs.Length >= 2)
            {
                end = int.Parse(subs[1]);
            }
            return value.SubstringUnsafe(start, value.Length - start - end);
        }
    }

    public class HtmlFetchingPropertyValue
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 属性值，字符串或字符串数组。
        /// </summary>
        public object value { get; set; }
    }
}
